package com.bx.implatform.ucc.controller;

import com.bx.implatform.ucc.dto.CreateRoomParamVO;
import com.bx.implatform.ucc.dto.JrhListParamDTO;
import com.bx.implatform.ucc.entity.UccLoginSysEntity;
import com.bx.implatform.ucc.entity.UccTerminalParamEntity;
import com.bx.implatform.ucc.service.UccApiService;
import com.fasterxml.jackson.databind.JsonNode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 门户控制器
 */
@RestController
@RequestMapping("/imApi/ucc")
@Tag(name = "融合通讯接口")
@RequiredArgsConstructor
public class UccApiController {

    @Resource
    private UccApiService uccApiService;

    @PostMapping("/system-service/api-v1.0/SysUser/login")
    @Operation(summary = "获取融合通讯视频模块Token", description = "获取融合通讯视频模块Token")
    public JsonNode uccToken() {
        return uccApiService.getUccToken();
    }

    @GetMapping("/control/20140901/infs/login.json")
    @Operation(summary = "获取融合通讯control系统token", description = "获取融合通讯control系统token")
    public JsonNode sysToken() {
        return uccApiService.sysToken();
    }

    @PostMapping("/control/api_v2.2.5/employee/query/queryAll.json")
    @Operation(summary = "终端管理", description = "终端管理，目的在于获取分机号用户视频通话呼叫")
    public JsonNode queryAll(@RequestBody UccTerminalParamEntity params) {
        return uccApiService.queryAll(params);
    }

    @GetMapping("/validateCode/get.json")
    @Operation(summary = "获取验证码", description = "获取验证码")
    public JsonNode validateCode() {
        return uccApiService.validateCode();
    }

    @PostMapping("/control/validateCode/validate.json")
    @Operation(summary = "验证验证码", description = "验证验证码")
    public JsonNode validate(@Valid @RequestBody UccLoginSysEntity sysLoginSysEntity) {
        return uccApiService.validate(sysLoginSysEntity);
    }

    @GetMapping("/video-service/api-v1.0/Camera/emplCamera/queryAllEmplCamera")
    @Operation(summary = "获取系统内所有的视频设备数据", description = "获取系统内所有的视频设备数据")
    public JsonNode queryAllEmplCamera() {
        return uccApiService.queryAllEmplCamera();
    }

    @GetMapping("/video-service/api-v1.0/Camera/getRTMP")
    @Operation(summary = "获取视频设备的视频流", description = "获取视频设备的视频流")
    public JsonNode getRTMP(
            @Parameter(description = "摄像头IP", required = true, example = "192.168.1.100")
            @RequestParam String ip,

            @Parameter(description = "摄像头ONVIF端口(默认80)", example = "80")
            @RequestParam(defaultValue = "80") String port,

            @Parameter(description = "摄像头ONVIF账号", required = true, example = "admin")
            @RequestParam String user,

            @Parameter(description = "摄像头ONVIF账号密码", required = true, example = "password123")
            @RequestParam String password,

            @Parameter(description = "转码画质", schema = @Schema(allowableValues = {"HIGH", "MIDDLE", "LOW", "IDHIGH", "IDLOW"}))
            @RequestParam(required = false) String rs,

            @Parameter(description = "通道(默认1)", example = "1")
            @RequestParam(defaultValue = "1") String channel) {
        return uccApiService.getRTMP(ip, port, user, password, rs, channel);
    }

    @GetMapping("/video-service/api-v2.0/monitor/counts")
    @Operation(summary = "获取视频通话路数", description = "获取当前系统中的视频通话路数")
    public JsonNode videoCounts() {
        return uccApiService.videoCounts();
    }

    @PostMapping("/control/20180628/infs/getAccessnumList.json")
    @Operation(summary = "获取接入号列表", description = "融合通讯获取接入号列表")
    public JsonNode jrhList(@Valid @RequestBody JrhListParamDTO dto) {
        return uccApiService.jrhList(dto);
    }

    @PostMapping("/control/api_v2.2.5/conference/add.json")
    @Operation(summary = "创建固定会议室", description = "融合通讯创建固定会议室")
    public JsonNode createRoom(@Valid @RequestBody CreateRoomParamVO vo) {
        return uccApiService.createRoom(vo);
    }

    @PostMapping("/control/api_v2.2.5/conference/delete.json")
    @Operation(summary = "删除固定会议室", description = "融合通讯删除固定会议室")
    public JsonNode deleteRoom(@NotNull(message = "会议室ID不能为空") @RequestParam Integer roomId) {
        return uccApiService.deleteRoom(roomId);
    }

    @GetMapping("/control/api_v2.2.5/conference/query/all.json")
    @Operation(summary = "获取会议室列表", description = "融合通讯获取会议室列表")
    public JsonNode listRoom(
            @Parameter(description = "会议室接入号", example = "8001")
            @RequestParam(required = false) String accessNum,

            @Parameter(description = "会议室名称", example = "技术部会议室")
            @RequestParam(required = false) String name,

            @Parameter(description = "排序字段", schema = @Schema(
                    description = "排序字段，默认值为id",
                    allowableValues = {"id", "name", "accessNum", "createTime"},
                    defaultValue = "id"
            ))
            @RequestParam(defaultValue = "id") String properties,

            @Parameter(description = "排序方式", schema = @Schema(
                    description = "排序方式，正序ASC倒序DESC默认为倒序",
                    allowableValues = {"ASC", "DESC"},
                    defaultValue = "DESC"
            ))
            @RequestParam(defaultValue = "DESC") String direction,

            @Parameter(description = "当前页码", example = "1",
                    schema = @Schema(minimum = "1", defaultValue = "1"))
            @RequestParam(defaultValue = "1") Integer currPage,

            @Parameter(description = "每页数据条数", example = "10",
                    schema = @Schema(minimum = "1", maximum = "100", defaultValue = "10"))
            @RequestParam(defaultValue = "10") Integer pageLimit,

            @Parameter(description = "用户ID（配置了IP白名单时此字段必填）", example = "user123")
            @RequestParam(required = false) String userId) {
        return uccApiService.listRoom(accessNum, name, properties, direction, currPage, pageLimit, userId);
    }

    @GetMapping("/system-service/api-v1.0/LinkMan/getAllLinkMans")
    @Operation(summary = "获取全部/分组联系人", description = "获取全部/分组联系人")
    public JsonNode getAllLinkMans(@RequestParam(required = false) String groupId) {
        return uccApiService.getAllLinkMans(groupId);
    }

    @GetMapping("/control/api-v2.3.0/user/query.json")
    @Operation(summary = "21.1 查询调度员", description = "此接口用于查询系统中的调度员数据。支持分页及搜索")
    public JsonNode getUserList(
            @Parameter(description = "调度员ID")
            @RequestParam(required = false) String id,

            @Parameter(description = "账号，模糊匹配")
            @RequestParam(required = false) String userId,

            @Parameter(description = "名称，模糊匹配")
            @RequestParam(required = false) String userName,

            @Parameter(description = "分区名，模糊匹配")
            @RequestParam(required = false) String deptName,

            @Parameter(description = "是否管理员，0否1是")
            @RequestParam(required = false) String isAdmin,

            @Parameter(description = "绑定的左右话机，模糊匹配")
            @RequestParam(required = false) String exten,

            @Parameter(description = "当前页，默认第一页", example = "1")
            @RequestParam(defaultValue = "1") String currPage,

            @Parameter(description = "每页条数，默认10条", example = "10")
            @RequestParam(defaultValue = "10") String pageLimit,

            @Parameter(description = "排序字段，可传值：id、userid、username、last_logintime")
            @RequestParam(required = false) String properties,

            @Parameter(description = "排序方式 ASC DESC", examples = {
                    @ExampleObject(name = "升序", value = "ASC"),
                    @ExampleObject(name = "降序", value = "DESC")
            })
            @RequestParam(required = false) String direction) {
        return uccApiService.getUserList(id, userId, userName, deptName, isAdmin, exten, currPage, pageLimit, properties, direction);
    }

    @GetMapping("/control/20210628/infs/getAllCdrs.json")
    @Operation(summary = "7.1 获取话单", description = "此接口用于获取系统中所有的话单列表数据")
    public JsonNode getAllCdrs(
            @Parameter(description = "主叫号", required = true)
            @RequestParam String caller,

            @Parameter(description = "被叫号")
            @RequestParam(required = false) String callee,

            @Parameter(description = "开始时间（不填写时间条件默认查当天的）")
            @RequestParam(required = false) String startTime,

            @Parameter(description = "结束时间")
            @RequestParam(required = false) String endTime) {
        return uccApiService.getAllCdrs(caller, callee, startTime, endTime);
    }

    @GetMapping("/video-conference-system/vcs/public/meetinglog")
    @Operation(summary = "获取视频会议录像记录", description = "获取视频会议录像记录")
    public JsonNode meetinglog(
            @Parameter(description = "对接系统的密钥")
            @RequestParam String secretKey,

            @Parameter(description = "会议室名称，模糊匹配")
            @RequestParam String meetingName,

            @Parameter(description = "当前页，默认第一页", example = "1")
            @RequestParam(defaultValue = "1") Integer currPage,

            @Parameter(description = "每页条数，默认10条", example = "10")
            @RequestParam(defaultValue = "10") Integer pageLimit,

            @Parameter(description = "排序字段，可传值：id、userid、username、last_logintime")
            @RequestParam(defaultValue = "id") String properties,

            @Parameter(description = "排序方式 ASC DESC", examples = {
                    @ExampleObject(name = "升序", value = "ASC"),
                    @ExampleObject(name = "降序", value = "DESC")
            })
            @RequestParam(defaultValue = "DESC") String direction) {
        return uccApiService.meetinglog(secretKey, meetingName, currPage, pageLimit, properties, direction);
    }

    @GetMapping("/control/20210615/infs/downCdrFile.json")
    @Operation(summary = "7.2 下载话单录音文件", description = "此接口用于下载话单录音文件数据")
    public ResponseEntity<byte[]> downCdrFile(
            @RequestParam(name = "callUuid参数值填写获取话单接口返回值中的callId值") String callUuid) {
        byte[] fileData = uccApiService.downCdrFile(callUuid);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDisposition(ContentDisposition.attachment()
                .filename("cdr_" + callUuid + ".wav")
                .build());

        return ResponseEntity.ok()
                .headers(headers)
                .body(fileData);
    }

    @GetMapping("/system-service/api-v2.0/conference/members")
    @Operation(summary = "14.6 查询固定会议室成员", description = "此接口用于在系统中查询固定会议室成员")
    public JsonNode members(
            @Parameter(description = "固定会议室的ID")
            @RequestParam Integer confId,

            @Parameter(description = "会议室名称")
            @RequestParam String confName) {
        return uccApiService.members(confId, confName);
    }
}
